'''
Created on 24/03/2013

@author: Elfi
'''

import sys

# Configuration for downloader module
attr_workers_threads = 3

################################################################################################
'''
   Equal sequence searcher
'''
def matchingString(s1, s2):
    '''Compare 2 sequence of strings and return the matching sequences concatenated'''
    from difflib import SequenceMatcher
    
    matcher = SequenceMatcher(None, s1, s2)
    output = ""
    for (i,j,n) in matcher.get_matching_blocks():
        output += s1[i:i+n]
    
    return output

################################################################################################
'''
    Helpers for calling with a sequence of strings
    Single thread call
'''
def matchingStringSequence( seq ):
    '''
        Compare between pairs up to final result
    '''
    try:
        matching = seq[0]
        for s in seq[1:len(seq)]:
            matching = matchingString(matching, s)
        
        return matching
    except TypeError:
        return ""

################################################################################################
'''
    Helpers for calling in parallel the matcher
'''
def helper( t ):
    '''Simple and single function for pooling'''
    return matchingString(t[0], t[1])


def matchingStringSequenceParallel( seq ):
    '''
        Compare between pairs up to final result
    '''
    from multiprocessing import Pool 
    
    try:
        if len(seq) < 2:
            return seq[0]
        
        # turn the sequence in a non-odd one
        tail = None
        if len(seq) % 2 > 0:
            tail = seq[-1]
        
        # Create tuples to parallel the comparison in pairs
        zipped = zip( seq[0::2], seq[1::2] )
        
        # Create the pool and call all of them in parallel
        pool = Pool( attr_workers_threads )
        result = pool.map(helper, zipped)

        if tail:
            result.append(tail)
        
        return matchingStringSequenceParallel(result)

    except TypeError:
        return ""

################################################################################################
'''
    Analyze the URL contents in search for caching areas
'''
def analyze(url):
    '''
        Identify areas for caching giving a URL.
        The method works as follows:
        1. Gets all the URL contents from the DB for comparison
        2. Set first content as the initial matching string
        3. For each remaining content, get matching string with previous result
        4. Final matching string will be the areas to cache
    '''
    from storage.db_connection import DBConnection
    
    siteContents = DBConnection.getInstance().siteContents(url)
    
    strMatch = matchingStringSequence([i["value"] for i in siteContents])
    
    print str(strMatch.encode("ascii", "ignore"))

if __name__ == '__main__':
    if sys.argv[1]:
        analyze(sys.argv[1])
    else:
        print "Usage: python %s url" % sys.argv[0]
